using Furion.DatabaseAccessor;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

using Tool;

namespace Abc.Core.Entities
{
    /// <summary>
    /// 角色
    /// </summary>
    [TablePrefix("Web.")]
    [Comment("角色")]
    public class Role : FullEntity, IEntityTypeBuilder<Role>, IEntitySeedData<Role>
    {
        /// <summary>
        /// 标识
        /// </summary>
        [Comment("标识")]
        public string Code { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        [Comment("名称")]
        public string Name { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        [Comment("排序")]
        public int Sort { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        [Comment("是否启用")]
        public bool IsEnable { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        [Comment("备注")]
        public string Remark { get; set; }

        #region ref

        public ICollection<Permission> Permissions { get; set; }
        public ICollection<RolePermission> RolePermissions { get; set; }

        /// <summary>
        /// 多对多
        /// </summary>
        public ICollection<User> Users { get; set; }

        public List<UserRole> UserRoles { get; set; }

        #endregion ref

        public void Configure(EntityTypeBuilder<Role> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.HasIndex(o => new { o.Name });

            entityBuilder.HasMany(o => o.Permissions).WithMany(o => o.Roles).UsingEntity<RolePermission>(
                u => u.HasOne(c => c.Permission).WithMany(c => c.RolePermissions).HasForeignKey(c => c.PermissionId)
                , u => u.HasOne(c => c.Role).WithMany(c => c.RolePermissions).HasForeignKey(c => c.RoleId)
                , u =>
                {
                    u.HasKey(c => new { c.PermissionId, c.RoleId });
                    //u.HasData(new {AdminId=1,RoleId=1 })//数据种子
                    // u.Property(pt => pt.CreationTime).HasDefaultValueSql("CURRENT_TIMESTAMP"); //每种数据库支持不一样
                }
            );
        }

        public IEnumerable<Role> HasData(DbContext dbContext, Type dbContextLocator)
        {
            return new List<Role>()
            {
            };
        }
    }
}